#!/usr/bin/env bash

# Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

srcdir=${pkg}

if [ -d build ]; then
    rm -r build
fi
mkdir -p build || exit 1
cd build || exit 1

if [ ! -z "${feature_musl}" ]; then
    host_triple=${arch}-unknown-linux-musl
    if [ -n "${feature_cross_aarch64}" ]; then
        target_triple=aarch64-unknown-linux-musl
    else
        target_triple=${arch}-unknown-linux-musl
    fi
else
    host_triple=${arch}-unknown-linux-gnu
    if [ -n "${feature_cross_aarch64}" ]; then
        target_triple=aarch64-unknown-linux-gnu
    else
        target_triple=${arch}-unknown-linux-gnu
    fi
fi

extra_args=()
if [ "${arch_bits}" == "64" ]; then
    extra_args+=(--disable-multilib)
fi
if [ ${impure} -eq 0 ]; then
    extra_args+=(--host=${host_triple})
    extra_args+=(--target=${target_triple})

    if [ ${host_toolchain} -eq 1 ]; then
        extra_args+=(--enable-bootstrap=no)
    fi

    if [ -n "${feature_cross_aarch64}" ]; then
        if [ -n "${feature_only_gcc}" ]; then
            extra_args+=(--with-newlib)
            extra_args+=(--without-headers)
            extra_args+=(--disable-threads)
            extra_args+=(--disable-shared)
        else
            extra_args+=(--with-sysroot=/cross/aarch64)
        fi
    fi
else
    extra_args+=(--enable-bootstrap=yes)
    extra_args+=(--with-gmp=${pkgs_root}/${dep_gmp}-impure/usr)
    extra_args+=(--with-mpfr=${pkgs_root}/${dep_mpfr}-impure/usr)
    extra_args+=(--with-mpc=${pkgs_root}/${dep_mpc}-impure/usr)
fi

if [ ${host_toolchain} -eq 1 ]; then
    export LDFLAGS="${LDFLAGS} -Wl,-rpath-link=/usr/lib:/usr/lib64:/lib:/lib64"

    export NATIVE_TOOL_DIR=/usr/bin/gcc-build/

    ln -sf gcc /usr/bin/cc
    ln -sf cc /usr/bin/${host_triple}-cc
    ln -sf gcc /usr/bin/${host_triple}-gcc
    ln -sf g++ /usr/bin/${host_triple}-g++
    ln -sf gfortran /usr/bin/${host_triple}-gfortran
fi

if [ -n "${feature_cross_aarch64}" ] && [ -z "${feature_only_gcc}" ]; then
    for exe in gcc g++ c++; do
        mv /usr/bin/${target_triple}-${exe} /usr/bin/${target_triple}-${exe}-wrapped

        cat > /usr/bin/${target_triple}-${exe} << EOF
#!/usr/bin/env bash
if [ "\$1" != "-cc1" ]; then
    args=()
    for arg in \$@; do
        if [ \${arg} != "-I/usr/include" ]; then
            args+=(\${arg})
        fi
    done

    ${target_triple}-${exe}-wrapped -isystem /cross/aarch64/usr/include -B /cross/aarch64/usr/lib -Wl,-rpath-link=/cross/aarch64/usr/lib:/cross/aarch64/usr/lib64 "\${args[@]}"
else
    ${target_triple}-${exe}-wrapped "\$@"
fi
EOF

        chmod a+x /usr/bin/${target_triple}-${exe}
    done

    ln -sfv ${target_triple}-gcc /usr/bin/${target_triple}-cc

    if [ -z "${feature_musl}" ]; then
        for ldscript in libc.so libm.so libm.a; do
            mv /cross/aarch64/usr/lib/$ldscript /cross/aarch64/usr/lib/$ldscript.back
            cp /cross/aarch64/usr/lib/$ldscript.back /cross/aarch64/usr/lib/$ldscript
            sed -i "s/\/usr/\/cross\/aarch64\/usr/g" /cross/aarch64/usr/lib/$ldscript
        done

        # FIXME
        if [ -L /usr/${target_triple}/include ]; then
            rm /usr/${target_triple}/include
        fi
    fi
fi

if [ "${CC}" = "clang" ]; then
    export CC=gcc
    export CXX=g++
fi

../${srcdir}/configure \
    --prefix=/usr \
    ${extra_args[@]} \
    || exit 1

if [ -n "${feature_only_gcc}" ]; then
    # FIXME: GCC_FOR_TARGET is needed for all-gcc, but it is produced by install-gcc
    make -j ${MAX_JOBS} all-gcc
    make -j ${MAX_JOBS} install-gcc -k
    make -j ${MAX_JOBS} install-gcc -k
    make -j ${MAX_JOBS} all-gcc install-gcc

    # fixes for libgcc
    ln -sfv ${target_triple}-gcc /usr/bin/${target_triple}-cc
    cp ./gcc/include/*.h /usr/lib/gcc/${target_triple}/${pkgver}/include/

    make -j ${MAX_JOBS} all-gcc all-target-libgcc
else
    make -j ${MAX_JOBS}
fi
